package com.runshu.runshuwulian.ui.viewhelper;

import com.runshu.runshuwulian.base.BaseActivity;
import com.runshu.runshuwulian.base.baselistfragment.BaseListHelper;
import com.runshu.runshuwulian.base.net.BaseObserver;
import com.runshu.runshuwulian.base.net.RetrofitFactory;
import com.runshu.runshuwulian.bean.BaseBean;
import com.runshu.runshuwulian.bean.LabelBean;
import com.runshu.runshuwulian.bean.ParentLabelBean;
import com.runshu.runshuwulian.bean.XiaoHaoListBean;
import com.runshu.runshuwulian.bean.XiaohaoMenuBean;
import com.runshu.runshuwulian.ui.fragment.XiaoHaoFrg;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function3;


public class XiaoHaoHelper extends BaseListHelper {
    XiaoHaoFrg xiaoHaoFrg;
    BaseActivity activity;

    public XiaoHaoHelper(XiaoHaoFrg xiaoHaoFrg, BaseActivity activity) {
        this.xiaoHaoFrg = xiaoHaoFrg;
        this.activity = activity;
    }

    public void getListInfo() {
        Observable<BaseBean<List<LabelBean>>> net1 = activity.api().getCostOrderStatus().compose( RetrofitFactory.threadTransformer() );
        Observable<BaseBean<List<LabelBean>>> net3 = activity.api().getAllStatus().compose( RetrofitFactory.threadTransformer() );
        Observable<BaseBean<List<ParentLabelBean>>> net2 = activity.api().getHospitalDepartmentList().compose( RetrofitFactory.threadTransformer() );
        Observable<XiaohaoMenuBean> combineRequest = Observable.zip( net1, net2, net3, new Function3<BaseBean<List<LabelBean>>, BaseBean<List<ParentLabelBean>>, BaseBean<List<LabelBean>>, XiaohaoMenuBean>() {
            @Override
            public XiaohaoMenuBean apply(BaseBean<List<LabelBean>> listBaseBean, BaseBean<List<ParentLabelBean>> listBaseBean2, BaseBean<List<LabelBean>> listBaseBean3) throws Exception {
                XiaohaoMenuBean xiaohaoMenuBean = new XiaohaoMenuBean( (listBaseBean != null) ? listBaseBean.getData() : null,
                        (listBaseBean2 != null) ? listBaseBean2.getData() : null,
                        (listBaseBean3 != null) ? listBaseBean3.getData() : null );
                return xiaohaoMenuBean;
            }
        } );
        combineRequest.subscribe( new Observer<XiaohaoMenuBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(XiaohaoMenuBean xiaohaoMenuBean) {
                xiaoHaoFrg.initMenu( xiaohaoMenuBean );
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        } );

    }

    JSONObject params = new JSONObject();

    public JSONObject getParams() {
        return params;
    }

    public void setParams(JSONObject params) {
        this.params = params;
        refreshData();
    }

    @Override
    public void getData(boolean isRefresh) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put( "current", currentPage );
            if (params != null) {
                jsonObject.put( "params", params );
            }
            jsonObject.put( "size", PAGE_SIZE );
        } catch (JSONException e) {
            e.printStackTrace();
        }
        activity.api().getSelectSmallConsumeOrderk( activity.getRequestBody( jsonObject ) )
                .compose( RetrofitFactory.threadTransformer() )
                .subscribe( new BaseObserver<XiaoHaoListBean>() {
                    @Override
                    protected void onSuccees(BaseBean<XiaoHaoListBean> response) throws Exception {
                        if (isRefresh) {
                            if (response.getData() != null)
                                xiaoHaoFrg.setListData( response.getData().getRecords() );
                        } else {
                            if (response.getData() != null)
                                xiaoHaoFrg.addListData( response.getData().getRecords() );
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        xiaoHaoFrg.showShortToast( e.getMessage() );
                        currentPage--;
                    }
                } );
    }
}
